﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using BLL;
using Model;
using Ysb.Components;
public partial class Admin_BillStatePl : System.Web.UI.Page
{
    DataTable dt = null;
    DataTable dt2 = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!AdminCheck.Check(this, 1))
        {
            Response.Redirect(AdminCheck.RedirectPage);
            return;
        }
        if (!Page.IsPostBack)
        {
           




          using ( dt = new PublicSqlService().GetData("BSID,Description", "Tbl_BillState", ""))

        {

            
            using ( DataTable ndt = new PublicSqlService().GetData("ID,CompanyName", "TblDomesticShipping", ""))
{

            DataRow dr = ndt.NewRow();
            dr["ID"] = "0";
            dr["CompanyName"] = "";

            ndt.Rows.InsertAt(dr, 0);
            dt2 = ndt;
            string lb = Request.QueryString["no"].ToString();
            List<TnoInfo> ls = new List<TnoInfo>();
            ls = new WayBillService().GetList(Request.QueryString["no"].ToString(),1);
            string sql1 = "'False' as Sep,TrackingNo,StateId ,UPEXState";
            string sql2 = "'True' as Sep,SepNo as TrackingNo, State as StateId ,SBState as UPEXState";
            DataTable ndt1 = new WayBillService().GetBillMess(sql1, sql2, ls, 1);
            list.DataSource = ndt1;
            list.DataBind();
  }

        }


          
        }
    }

    public DataTable GetState()
    {
        return dt;
    }
    public DataTable GetZygfs()
    {
        return dt2;
    }
    protected void okbtn_Click(object sender, ImageClickEventArgs e)
    {

        int sum = 0;
        Hashtable hs = new Hashtable();
        for (int i = 0; i < list.Items.Count; i++)
        {
            Label sep = (Label)list.Items[i].FindControl("seplb");
            Label no = (Label)list.Items[i].FindControl("no_txt");
            TextBox sj = (TextBox)list.Items[i].FindControl("datestate");
            TextBox dh = (TextBox)list.Items[i].FindControl("zydh");
            DropDownList zt = (DropDownList)list.Items[i].FindControl("imagelist");
            DropDownList gs = (DropDownList)list.Items[i].FindControl("zygs");
            TextBox bz = (TextBox)list.Items[i].FindControl("beizhutxt");
            if (zt.SelectedValue.Trim() != "")
            {
                hs.Clear();
                hs.Add("SepNo", no.Text.Trim());
                hs.Add("Date", new Tools().StringToTime(sj.Text.Trim()));
                hs.Add("StateID", zt.SelectedValue.Trim());
                hs.Add("RecordDate", DateTime.Now);
                hs.Add("RecorderID", Session["uid"].ToString());
                hs.Add("CompanyID", gs.SelectedValue);
                hs.Add("CompanyNo", dh.Text.Trim());
                hs.Add("Remark", bz.Text);
                if (new PublicSqlService().Insert(hs, "Tbl_BillStateRec"))
                {
                    
                    sum++;
                    if (sep.Text.Trim() == "")
                    {
                  
 
                        hs.Clear();
                        hs.Add("StateId", zt.SelectedValue.Trim());
                        hs.Add("UPEXState", zt.SelectedItem.Text.Trim());
                        new PublicSqlService().Update(hs, "Tbl_WayBill", "TrackingNo='" + no.Text.Trim() + "'");
                    }
                    else
                    {
                 
                        hs.Clear();
                        hs.Add("State", zt.SelectedValue.Trim());
                        hs.Add("SBState", zt.SelectedItem.Text.Trim());
                        new PublicSqlService().Update(hs, "Tbl_SepBill", "SepNo='" + no.Text.Trim().Replace("SepNo", "")+"'");

                        if (zt.SelectedValue.Trim() != "1")
                        {  //更新主单状态
                            new WayBillService().UpSatate(no.Text.Trim().Replace("SepNo", ""));
                        }

                    }
                }

            }


        }

        Tools.OpenWindow(this, "状态设置成功！共设置：" + sum.ToString() + "条数据");


    }
}
